package com.ziq.zcamp.community

import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.ziq.base.base.BaseActivity
import com.ziq.base.extension.dp
import com.ziq.zcamp.R
import com.ziq.zcamp.bean.post.PostSubject
import com.ziq.zcamp.databinding.ActivitySubjectTabBinding
import com.ziq.zcamp.manager.GlobalContextManager
import com.ziq.zcamp.view.CustomLinePagerIndicator
import com.ziq.zcamp.view.OrderPagerTitleView
import net.lucode.hackware.magicindicator.FragmentContainerHelper
import net.lucode.hackware.magicindicator.buildins.UIUtil
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator
import com.ziq.base.extension.binding
import com.ziq.base.extension.click
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class SubjectTabActivity: BaseActivity() {

    companion object {
        const val TARGET_TAB_DATA = "tab_data"
        const val TARGET_TAB_ID = "tab_index"

        fun open(context: Context, data: ArrayList<PostSubject>, targetTabId:Int){
            val intent = Intent(context, SubjectTabActivity::class.java)
            intent.putExtra(TARGET_TAB_DATA, data)
            intent.putExtra(TARGET_TAB_ID, targetTabId)
            GlobalContextManager.startActivity(context, intent)
        }
    }

    private val binding: ActivitySubjectTabBinding by binding()
    var initTabIndex = 0
    var dataList = arrayListOf<PostSubject>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        try {
            if(intent.hasExtra(TARGET_TAB_DATA)){
                dataList  = intent.getSerializableExtra(TARGET_TAB_DATA) as ArrayList<PostSubject>
            }
            if(intent.hasExtra(TARGET_TAB_ID)){
                val targetId  = intent.getIntExtra(TARGET_TAB_ID, 0)
                if(dataList.isNotEmpty()){
                    for((index, subject) in dataList.withIndex()){
                        if(subject.id == targetId){
                            initTabIndex = index
                            break
                        }
                    }
                }
            }
        }catch (e:Exception){
            println("$e")
        }
        binding.root
        binding.back.click {
            finish()
        }

        initMagicIndicator()
        initViewPager()

        handleTabSelected(initTabIndex, false)
    }


    private val mFragmentContainerHelper = FragmentContainerHelper()
    private fun initMagicIndicator() {
        val magicIndicator = binding.magicIndicator
        val commonNavigator = CommonNavigator(this)
        commonNavigator.notifyDataSetChanged()
        commonNavigator.isAdjustMode = false
        commonNavigator.adapter = object : CommonNavigatorAdapter() {
            override fun getCount(): Int {
                return dataList.size
            }

            override fun getTitleView(context: Context, index: Int): IPagerTitleView {
                val simplePagerTitleView = OrderPagerTitleView(context)
                simplePagerTitleView.setText(dataList[index].name?:"")
                simplePagerTitleView.selectedColor = Color.parseColor("#333333")
                simplePagerTitleView.normalColor = Color.parseColor("#999999")
                simplePagerTitleView.textSize = 14f
                simplePagerTitleView.setPadding(16.dp, 0, 16.dp,0)
                simplePagerTitleView.setOnClickListener {
                    handleTabSelected(index)
                }
                return simplePagerTitleView
            }

            override fun getIndicator(context: Context): IPagerIndicator? {
                val indicator = CustomLinePagerIndicator(context)
                indicator.mode = LinePagerIndicator.MODE_EXACTLY
                indicator.setImageSrc(R.drawable.icon_place_dialog_tab_index)
                indicator.lineHeight = UIUtil.dip2px(context, 4.0).toFloat()
                indicator.lineWidth = UIUtil.dip2px(context, 30.0).toFloat()
                indicator.setColors(Color.parseColor("#000000"))
                return indicator
            }
        }
        commonNavigator.leftPadding = 4.dp
        commonNavigator.rightPadding = 4.dp
        magicIndicator.navigator = commonNavigator
        mFragmentContainerHelper.attachMagicIndicator(magicIndicator)
    }

    private fun initViewPager() {
        binding.viewPager.adapter = object : FragmentStateAdapter(this) {
            override fun getItemCount(): Int {
                return dataList.size
            }

            override fun createFragment(position: Int): Fragment {
                return SubjectContentListFragment.createFragment(dataList[position].id?:0)
            }
        }
        binding.viewPager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                handleTabSelected(position, isForViewPagerScroll = true)
            }
        })
        binding.viewPager.isUserInputEnabled = true
    }


    private var currentTabIndex = -1
    fun handleTabSelected(index: Int, isForViewPagerScroll: Boolean = false){
        if(currentTabIndex != index && index >= 0){
            currentTabIndex = index
            mFragmentContainerHelper.handlePageSelected(index)
            if(!isForViewPagerScroll){
                binding.viewPager.setCurrentItem(index, false)
            }
        }
    }


}